﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Furion;
using Furion.DatabaseAccessor;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using YShop.Application.Contracts.Shop;
using YShop.Core.Entites.Shop;
using YShop.Core.Managers.Caches;

namespace YShop.Application.Shop
{
    /// <summary>
    /// 商品属性
    /// </summary>
    [ApiDescriptionSettings("Shop")]
    [Route("api/product/attribute")]
    public class ProductAttributeService : CrudService<ProductAttribute, ProductAttributeDto, ProductAttributeDto, PageSearchDto, ProductAttributeDto, ProductAttributeDto>, IBaseService
    {
        private readonly IRepository<ProductAttribute> _repository;
        public override string EntitiesCacheKey => CacheKeys.ProductAttributes;
        public override List<ProductAttribute> EntitiesCache => App.GetService<CacheManager>().GetOrCreate(CacheKeys.ProductAttributes, _ => 
        {
            var results = _repository.Include(s => s.Items).ToList();
            results.ForEach(s => s.ItemJoin = string.Join(" / ", s.Items.Select(i=>i.Name)));
            return results;
        });
        public ProductAttributeService(IRepository<ProductAttribute> repository) : base(repository)
        {
            _repository = repository;
        }
    }
}
